package com.keda.dao;

import com.keda.model.Tag;
import com.keda.util.DBUtil;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TagDao {
    public List<Tag> queryTags() {

        Connection connection = null;
        try {
            // 建立连接
            connection = DBUtil.createConnection();

            // 查询数据库
            String sql = "select id, name, url from t_tag";
            QueryRunner queryRunner = new QueryRunner();
            List<Tag> tags = queryRunner.query(connection, sql, new BeanListHandler<Tag>(Tag.class));
            return tags;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        } finally {
            // 关闭
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
